package chabernac.android.wordbattle.paint.boggle;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.View;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.WordRasterContext;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.Word;
import chabernac.android.wordbattle.model.WordRaster;
import chabernac.android.wordbattle.model.iWordRasterListener;
import chabernac.android.wordbattle.paint.ColorFactory;
import chabernac.android.wordbattle.paint.iDrawable;

/**
 * http://stackoverflow.com/questions/4374141/android-drawbitmap-very-slow-when-using-argb-8888
 *
 */

public class BoggleSelectedWordsBitmapDrawable extends iDrawable {
  private final Paint myPaint = new Paint();
  private final Set<Word> myWordsAddedToBitmap = new HashSet<Word>();
  private final WordRaster myWordRaster;
  private WordBattleView myWordBattleView;
  
  private Bitmap myBitmap;
  private Canvas myCanvas;

  public BoggleSelectedWordsBitmapDrawable() {
    super();
    myWordRaster = WordRasterContext.getInstance().getMediator().getWordRaster();
    init();
    addListener();
  }
  
  private void init(){
    myPaint.setStrokeWidth( 18 );
    myPaint.setStyle(Style.FILL);
    myPaint.setAntiAlias(false);
    myPaint.setMaskFilter( new BlurMaskFilter( 5, BlurMaskFilter.Blur.NORMAL) );
    myPaint.setAlpha(255);
  }
  
  private void addListener(){
    myWordRaster.addListener(new WordRasterListener());
  }
  
  private void addWordToBitmap(Word aWord, WordBattleView aView){
    myPaint.setColor(ColorFactory.getRandomColor());
    if(!myWordsAddedToBitmap.contains(aWord)){
      Letter thePreviousLetter = null;
      float thePreviousCenterX = -1;
      float thePreviousCenterY = -1;
      for(Letter theLetter : aWord.getLetters()){
        
        float theCenterX = (float)(WordBattleView.BORDER_SIZE + (theLetter.getColumn() + 0.5) * aView.getWidthPerColumn());
        float theCenterY = (float)(WordBattleView.BORDER_SIZE + (theLetter.getRow() + 0.5) * aView.getHeightPerRow());
        
        if(thePreviousLetter != null){
          //draw a rectangle from the center of the previsou letter to the center of this letter
          
          myCanvas.drawLine(thePreviousCenterX, thePreviousCenterY, theCenterX, theCenterY, myPaint);
        }
        
        thePreviousLetter = theLetter;
        thePreviousCenterX = theCenterX;
        thePreviousCenterY = theCenterY;
      }
    }
  }
  
  private void createBitmap(WordBattleView aView){
    myBitmap = Bitmap.createBitmap(aView.getWidth(), aView.getHeight(), Bitmap.Config.ARGB_8888);
    myCanvas = new Canvas(myBitmap);
    
    for(Word theWord : myWordRaster.getFoundWordsByLocalPlayer()){
      addWordToBitmap(theWord, aView);
    }
  }

  @Override
  public void paint(Canvas aCanvas, View aView) {
    myWordBattleView = (WordBattleView)aView;
    if(myBitmap == null){
      createBitmap((WordBattleView)aView);
    }
    myPaint.setAlpha(100);
    aCanvas.drawBitmap(myBitmap, 0, 0, myPaint);
  }
  
  public class WordRasterListener implements iWordRasterListener {

    @Override
    public void selectionChanged(List<Letter> aSelectedLetters) {
    }

    @Override
    public void wordFound(Word aWord) {
      addWordToBitmap(aWord, myWordBattleView);
    }
  }

}
